<?php
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_multi_level.php';


if (!empty($_GET['category_choosen_id'])) {
    $category_chosen_id = $_GET['category_choosen_id'];
    $expand_id_list = api_multi_level::get_expand_id_list_document_category($category_chosen_id);
//    var_dump($expand_id_list);
}

$root_list = api_multi_level::get_root_document_categories();

function multi_categories($expand_id_list) {
//    var_dump($expand_id_list);
    if (sizeof($expand_id_list) == 0) {
        return;
    }
    $list = api_multi_level::get_document_categories_by_parent_id($expand_id_list[count($expand_id_list) - 1]);
    unset($expand_id_list[count($expand_id_list) - 1]);
?>
<ul>
    <?php
    foreach ($list as $item) {
//        var_dump($expand_id_list);
        if (in_array($item['cat_id'], $expand_id_list)) {
        ?>
            <li>
                <a href="javascript:void(0)">
                    <i class="uk-icon-minus" onclick="filldata('<?php echo $item['cat_parent_id']; ?>');"></i>
                </a>
                <a href="document.php?cat_id=<?php echo $item['cat_id']; ?>"><i class="uk-icon-folder-open"></i> <?php echo $item['cat_name']; ?></a>
                <?php
                multi_categories($expand_id_list);
                ?>
            </li>
        <?php
        } else {
        ?>
            <li>
                <a href="javascript:void(0)">
                    <i class="uk-icon-plus" onclick="filldata('<?php echo $item['cat_id']; ?>');"></i>
                </a>
                <a href="document.php?cat_id=<?php echo $item['cat_id']; ?>"><i class="uk-icon-folder"></i> <?php echo $item['cat_name']; ?></a>
            </li>
        <?php
        }
    }
    ?>
</ul>

<?php
}

                    if (empty($expand_id_list)) {
                        $expand_id_list = array();
                    }
                    foreach ($root_list as $root_item) {

                        if (in_array($root_item['cat_id'], $expand_id_list)){
    ?>
                            <li>
                                <a href="javascript:void(0)">
                                    <i class="uk-icon-minus" onclick="filldata('0');"></i>
                                </a>
                                <a href="document.php?cat_id=<?php echo $root_item['cat_id']; ?>"><i class="uk-icon-folder-open"></i> <?php echo $root_item['cat_name']; ?></a>

                                <?php
                                if (!empty($expand_id_list)) {
                                    multi_categories($expand_id_list);
                                }
                                ?>

                            </li>
    <?php
                        } else {
                            ?>
                            <li>
                                <a href="javascript:void(0)">
                                    <i class="uk-icon-plus" onclick="filldata('<?php echo $root_item['cat_id']; ?>');"></i>
                                </a>
                                <a href="document.php?cat_id=<?php echo $root_item['cat_id']; ?>"><i class="uk-icon-folder"></i> <?php echo $root_item['cat_name']; ?></a>
                            </li>
                            <?php
                        }
                    }
    ?>